"""
@Author  : 吕申凯
@Time    : 2022/9/19 20:51
@File    : Tensorboard_test.py
@Function: 使用tensorboard来可视化数据

将数据添加到SummaryWriter中，则会生成对于的文件
"""

import numpy as np
from torch.utils.tensorboard import SummaryWriter

# 创建SummaryWriter对象
# 文件夹默认runs，
# 下一级文件夹命名以"_test_tensorboard"结尾
# event file文件命名以"1234"为后缀
writer = SummaryWriter(comment='_test_tensorboard', filename_suffix='1234')

# 生成100个数据
for x in range(100):
    # 添加数据到writer中
    writer.add_scalar(tag='y=2x', scalar_value=x * 2, global_step=x)
    writer.add_scalar('y=pow(2, x)', 2 ** x, x)
    # 添加一个数据组
    writer.add_scalars('data/scalar_group', {"xsinx": x * np.sin(x),
                                             "xcosx": x * np.cos(x),
                                             "arctanx": np.arctan(x)}, x)
writer.close()
